package cn.lzit.request.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CircleAreaServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println("<html><body bgcolor='pink'>");

        String strRadius = request.getParameter("radius"); // 获取半径参数
        double r = 0, area = 0, circumference = 0;

        try {
            r = Double.parseDouble(strRadius); // 将字符串转换为double类型
            area = Math.PI * r * r; // 计算面积
            circumference = 2 * Math.PI * r; // 计算周长
            out.print("<BR>半径:" + r);
            out.print("<BR>圆面积:" + area);
            out.print("<BR>圆周长:" + circumference);
            out.println("</body></html>");
        } catch (NumberFormatException e) {
            request.setAttribute("errorMessage", strRadius + ":半径数据不合法");
            // 请求转发到另一个servlet进行查询响应
            RequestDispatcher rd = request.getRequestDispatcher("ErrorServlet");
            rd.forward(request, response);
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}